package com.sw.c.schedule;//package com.example.demo.schedule;
//
//
//import com.alibaba.fastjson.JSON;
//import com.alibaba.fastjson.JSONObject;
//import com.alibaba.fastjson.TypeReference;
//import com.example.demo.pojo.entity.Forecast;
//import lombok.extern.slf4j.Slf4j;
//import org.apache.http.HttpResponse;
//import org.apache.http.client.methods.HttpPost;
//import org.apache.http.entity.StringEntity;
//import org.apache.http.impl.client.CloseableHttpClient;
//import org.apache.http.impl.client.HttpClients;
//import org.apache.http.util.EntityUtils;
//import org.springframework.context.annotation.Configuration;
//import org.springframework.scheduling.annotation.EnableScheduling;
//import org.springframework.scheduling.annotation.Scheduled;
//
//import javax.annotation.Resource;
//import java.io.IOException;
//import java.text.SimpleDateFormat;
//import java.util.ArrayList;
//import java.util.Date;
//import java.util.List;
//import java.util.Map;
//import java.util.stream.Collectors;
//
//
//@Slf4j
//@Configuration
//@EnableScheduling
////@ConditionalOnProperty(name = "powerRealDataAdd", havingValue = "true")
//public class CheckTaskSchedule {
//
////    @Scheduled(cron = "0 */1 * * * ?")  //每一小时执行一次
//    //@Scheduled(fixedRate = 60 * 1000)  //每半小时执行一次
//    private void addData() throws Exception {
//        log.info("----------开始扫描ht_check_task表----------");
//
//        // 创建HttpClient实例
//        CloseableHttpClient httpClient = HttpClients.createDefault();
//
//        // 设置请求的URL
//        String url = "http://111.198.29.34:8099//FloodAnalysis/ForecastAnalysisResult/getHSFXRIVLFLOWPlus";
//
//        // 创建HttpPost实例
//        HttpPost httpPost = new HttpPost(url);
//
//        // 设置请求的Header
//        httpPost.setHeader("Content-Type", "application/json");
//        // 添加Token到请求头
//        String token = "dataSourceqinghaiTokeneyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJxaW5naGFpMjAyNHFpbmdoYWlAMjAyNCJ9.-aI7sMwhedtcqrazoWRhxS9e-JMJfFmJ6vwJwWG1jyo";
//        httpPost.setHeader("Authorization", token); // 假设Token应该放在Authorization头中，并且通常以Bearer 开头
//
//        // 设置请求的参数
//        String jsonParams = "{\"ProgramType\": \"2\", \"ProgramID\": \"8\"}";
//        try {
//            StringEntity entity = new StringEntity(jsonParams, "UTF-8");
//
//            httpPost.setEntity(entity);
//
//            // 执行请求
//            HttpResponse response = httpClient.execute(httpPost);
//            // 获取响应的状态码
//            int statusCode = response.getStatusLine().getStatusCode();
//            // 判断状态码是否是200，以确认请求成功
//            if (statusCode == 200) {
//                // 获取返回的内容
//                String responseBody = EntityUtils.toString(response.getEntity());
//                String jsonData = responseBody; // 将上面的JSON字符串放在这里
//
//                // 解析整个JSON字符串为Map
//                Map<String, Object> resultMap = JSON.parseObject(jsonData, new TypeReference<Map<String, Object>>(){});
//                // 获取MaxFlow的值
//                double maxFlow = ((Number)resultMap.get("MaxFlow")).doubleValue();
//
//                List<String> timesList = (List<String>) resultMap.get("times");
//                String times = String.join(",", timesList);
//                // 解析JSON字符串
//                JSONObject jsonObject = JSON.parseObject(jsonData);
//                // 获取"data"字段
//                JSONObject dataObject = jsonObject.getJSONObject("data");
//                List<Forecast> list = new ArrayList<>();
//                Forecast forecast;
//                // 遍历"data"字段下的每一个条目（假设每个键代表一组数据）
//                for (String key : dataObject.keySet()) {
//                    JSONObject stationData = dataObject.getJSONObject(key);
//                    String flowResult = String.valueOf(stationData.getJSONArray("flowResult"));
//                    String[] flow = flowResult.replaceAll("^\\[|\\]$", "").split(",");
//                    String[] timeArr = times.split(",");
//
//                    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//                    for (int i = 0; i < timeArr.length; i++) {
//                        Date date = sdf.parse(timeArr[i]);
//                        Date forecastEndTime = sdf.parse(stationData.getString("ForecastEndTime"));
//                        Date allEndTime = sdf.parse(stationData.getString("AllEndTime"));
//
//                        forecast = new Forecast();
//                        forecast.setTimes(date);
//                        forecast.setSection(key);
//                        forecast.setForecastEndTime(forecastEndTime);
//                        forecast.setAllEndTime(allEndTime);
//                        forecast.setProgramID(stationData.getString("ProgramID"));
//                        forecast.setFlowResult(flow[i]);
//                        list.add(forecast);
//                    }
//                    System.out.println("对象数据："+list.toString());
//                }
//            } else {
//                System.out.println("错误返回code: " + statusCode);
//            }
//        } catch (Exception e) {
//            e.printStackTrace();
//        } finally {
//            try {
//                httpClient.close();
//            } catch (IOException e) {
//                e.printStackTrace();
//            }
//        }
//    }
//
//}
//
//
